﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Write.Api.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.Api.Application.CommandHander
{
    public class ApprovalCommandHander : IRequestHandler<ApprovalCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<WF_Instafre> instaresRequsitory;
        private readonly IBaseRequsitory<WF_Recode> recodeRequsitory;
        private readonly IBaseRequsitory<WF_Setting> settingRequsitory;
        private readonly IBaseRequsitory<Leave> leaveRequsitory;

        public ApprovalCommandHander(IMapper mapper, IBaseRequsitory<WF_Instafre> instaresRequsitory, IBaseRequsitory<WF_Recode> recodeRequsitory, IBaseRequsitory<WF_Setting> settingRequsitory, IBaseRequsitory<Leave> leaveRequsitory)
        {
            this.mapper = mapper;
            this.instaresRequsitory = instaresRequsitory;
            this.recodeRequsitory = recodeRequsitory;
            this.settingRequsitory = settingRequsitory;
            this.leaveRequsitory = leaveRequsitory;
        }

        /// <summary>
        /// 审批人审批申请
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(ApprovalCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            DateTime now = DateTime.Now;

            #region 流程表添加

            Leave leave = new Leave();

            var instafre = instaresRequsitory.GetAllAsync().Where(x => x.InstafreId == request.InstafreId).FirstOrDefault();
            var leavelist = leaveRequsitory.GetAllAsync().Where(x => x.LeaveId == request.LeaveId).FirstOrDefault();

            WF_Recode recode = new WF_Recode();
            recode.InstafreId = request.InstafreId;
            recode.LeaveId = request.LeaveId;
            recode.StarterNodeNo = request.Appstatus;
            recode.EnderNodeName = request.Appstatus.ToString();
            recode.UserId = Convert.ToInt32(instafre.NextOperNo);
            recode.UserName = instafre.NextOperName;
            recode.YiJian = request.YiJian;

            recode.CreateTime = now;

            if (instafre.NextStarterName == "主任审批")
            {
                recode.EnderNodeName = AppEnum.已完成.ToString();
                recode.StarterNodeNo = Convert.ToInt32(AppEnum.已完成);
            }
            else
            {
                recode.EnderNodeName = AppEnum.处理中.ToString();
                recode.StarterNodeNo = Convert.ToInt32(AppEnum.处理中);
            }

            if (request.Appstatus == (int)RecodeEnum.已拒绝)
            {
                leavelist.Appstatus = (int)RecodeEnum.已拒绝;
                recode.EnderNodeName = RecodeEnum.已拒绝.ToString();
                recode.StarterNodeNo = (int)RecodeEnum.已拒绝;
            }
            await recodeRequsitory.AddAsync(recode);
            leaveRequsitory.UpdateAsync(leavelist);

            #endregion

            #region  修改实例表
            instafre.StarterNode = (int)AppEnum.处理中;
            instafre.StarterName = AppEnum.处理中.ToString();

            var setting = settingRequsitory.GetAllAsync().Where(x => x.PextStarterNodeNo == instafre.NextStarterNodeNo).FirstOrDefault();


            if (instafre.StarterNode == (int)RecodeEnum.已拒绝)
            {
                instafre.StarterNode = (int)AppEnum.已完成;
                instafre.StarterName = AppEnum.已完成.ToString();
                 

                instafre.NextStarterNodeNo = "";
                instafre.NextOperName = "";
                instafre.NextOperNo = "";
                instafre.NextStarterName = "";
            }

            if (setting == null)
            {
                instafre.StarterNode = (int)AppEnum.已完成;
                instafre.StarterName = AppEnum.已完成.ToString();

                instafre.NextStarterName = "";
                instafre.NextStarterNodeNo = "";
                instafre.NextOperNo = "";
                instafre.NextOperName = "";
            }

            if (setting != null && request.Appstatus != (int)RecodeEnum.已拒绝)
            {
                instafre.PextOperName = instafre.NextOperName;
                instafre.PextOperNo = instafre.PextOperNo;
                instafre.PextStarterNodeNo = instafre.NextStarterNodeNo;
                instafre.PextStarterName = instafre.NextStarterName;

                instafre.NextStarterNodeNo = setting.NodeId;
                instafre.NextOperName = setting.UserName;
                instafre.NextOperNo = setting.UserId.ToString();
                instafre.NextStarterName = setting.NodeName;
            }

            instafre.UpdateTime = now;

            await instaresRequsitory.UpdateAsync(instafre);
            #endregion

            result.Code = ApiEnum.修改成功;
            result.Msg = ApiEnum.修改成功.ToString();

            return await Task.FromResult(result);
        }
    }
}
